package cn.org.intelli.zffserver.aop;


import cn.org.intelli.zffserver.exception.CustomException;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Configuration
public class AuthenticationTicketAop {
    /* 22 */   private static final Logger log = LoggerFactory.getLogger(AuthenticationTicketAop.class);


    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public void controllerGetMappingPointcut() {
    }


    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void controllerPostMappingPointcut() {
    }


    @Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public void controllerDeleteMappingPointcut() {
    }


    @Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)")
    public void controllerPutMappingPointcut() {
    }


    @Before("controllerGetMappingPointcut()")
    public void getAuthenticationTicketFormGetMapping(JoinPoint joinPoint) throws Exception {
        /* 43 */
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        /* 44 */
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        /* 45 */
        HttpServletRequest request = sra.getRequest();
        /* 46 */
        setAuthenticationTicket(request);
    }


    @Before("controllerPostMappingPointcut()")
    public void getAuthenticationTicketFormPostMapping(JoinPoint joinPoint) throws Exception {
        /* 52 */
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        /* 53 */
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        /* 54 */
        HttpServletRequest request = sra.getRequest();
        /* 55 */
        setAuthenticationTicket(request);
    }


    @Before("controllerDeleteMappingPointcut()")
    public void getAuthenticationTicketFormDeleteMapping(JoinPoint joinPoint) throws Exception {
        /* 61 */
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        /* 62 */
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        /* 63 */
        HttpServletRequest request = sra.getRequest();
    }

    private void setAuthenticationTicket(HttpServletRequest request) throws CustomException {
        /* 67 */
        String pin = (request.getHeader("X-Consumer-Username") != null) ? request.getHeader("X-Consumer-Username") : request.getHeader("x-credential-username");
        /* 68 */
        String token = request.getHeader("token");
        /* 69 */
        if (pin == null && token == null) {
            /* 70 */
            throw new CustomException("用户信息或token信息不能为空");
        }
        /* 72 */
        if (pin != null) {
            /* 73 */
            AuthenticationTicket.setTicket(new AuthenticationTicket(pin));
            /* 74 */
        } else if (token != null &&
                /* 75 */       StringUtils.equals(token, "zhangyan")) {
            /* 76 */
            AuthenticationTicket.setTicket(new AuthenticationTicket(token));
        }
    }
}

